layui.use(['layer','form','table','element','layedit','upload','laydate'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,layedit = layui.layedit
        ,laydate = layui.laydate
        ,upload = layui.upload;

    //表单验证
    form.verify({
            author:function (value) {
                if(value.length<1){
                    return "笔名长度必须大于1位";
                }
                if(value.length>10){
                    return "笔名长度必须小于10位";
                }
            },
            password: [/^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格']
            ,email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
    });


    //普通图片上传
    var uploadInst = upload.render({
        elem: '#update_cover_btn'
        ,url: '/admin/project/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.flag;
            var message = res.message;
            if(!flag){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("头像上传成功!");
                $("#cover_img").attr("value",res.data.url);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //普通图片上传
    var uploadInst2 = upload.render({
        elem: '#update_cover_btn2'
        ,url: '/admin/project/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo2').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.flag;
            var message = res.message;
            if(!flag){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("收款码上传成功!");
                $("#cover_img2").attr("value",res.data.url);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText2');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload2">重试</a>');
            demoText.find('.demo-reload2').on('click', function(){
                uploadInst2.upload();
            });
        }
    });

    //提交表单
    form.on('submit(form)',function (data) {

        layer.msg('修改中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        var paramData = {
            "uid":$("#id").val(),
            "password":$("#password").val(),
            "author":$("#author").val(),
            "email":$("#email").val(),
            "touxiang":$("#cover_img").val(),
            "pay":$("#cover_img2").val(),
            "realName":$("#realName").val()
        };
        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/user/update",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                // console.log("data=="+form_data)
                if(result.flag){
                    layer.msg(result.message);
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });
    });
});

